#include "stm32f10x.h"
#include "stdio.h"
#include "stdarg.h"

char Serial_RxPacket[100];
uint8_t Serial_RxFlag;

void Serial_Init(void) {
    // 1. RCC 开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    // 2. GPIO
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 3. USART
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 9600; // 波特率
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_Parity = USART_Parity_No; // 校验
    USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字长（位）
    USART_Init(USART1, &USART_InitStructure);
    // 4. 接收中断
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // 5. 开关控制
    USART_Cmd(USART1, ENABLE);
}

void Serial_SendByte(uint8_t Byte) {
    USART_SendData(USART1, Byte);
    // 等待 发送寄存器空标志位 置1
    while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE));
}

void Serial_SendArray(uint8_t* Array, uint16_t Length) {
    for (uint16_t i = 0; i < Length; i++) {
        Serial_SendByte(Array[i]);
    }
}

void Serial_SendString(char* String) {
    uint8_t i;
    for (i = 0; String[i] != '\0'; i++) {
        Serial_SendByte(String[i]);
    }
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y) {
    uint32_t Result = 1;
    while (Y--) {
        Result *= X;
    }
    return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length) {
    uint8_t i;
    for (i = 0; i < Length; i++) {
        Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
    }
}

// printf 底层调用函数 fput (* proteus 未生效)
int fput(int ch, FILE* f) {
    Serial_SendByte(ch);
    return ch;
}

// (* proteus 未生效)
void Serial_Printf(char* Format, ...) {
    char String[100];
    va_list arg;
    va_start(arg, Format);
    vsprintf(String, Format, arg);
    va_end(arg);
    Serial_SendString(String);
}

void USART1_IRQHandler(void) {
    static uint8_t RxState = 0;
    static uint8_t pRxPack = 0;
    if (USART_GetITStatus(USART1, USART_IT_RXNE)) {
        uint8_t RxData = USART_ReceiveData(USART1);
        if (RxState == 0) {
            if (RxData == '@' && Serial_RxFlag == 0) {
                RxState = 1;
                pRxPack = 0;
            }
        }
        else if (RxState == 1) {
            if (RxData == '\r') {
                RxState = 2;
            }
            else {
                Serial_RxPacket[pRxPack++] = RxData;
            }
        }
        else if (RxState == 2) {
            if (RxData == '\n') {
                RxState = 0;
                Serial_RxFlag = 1;
                Serial_RxPacket[pRxPack] = '\0';
            }
        }
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}
